import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// 插件
import { visualizer } from 'rollup-plugin-visualizer'
// import importToCDN from 'vite-plugin-cdn-import'
import externalGlobals from 'rollup-plugin-external-globals'

// https://vitejs.dev/config/
export default defineConfig({
  base: '/vue3-pc-2304', // 根据实际情况进行配置
  plugins: [
    vue({ reactivityTransform: true }),
    visualizer({ open: true }),
    // importToCDN({
    //   modules: [
    //     {
    //       name: 'vue',
    //       var: 'Vue',
    //       path: 'https://unpkg.com/vue@3.3.4'
    //     },
    //     {
    //       name: 'element-plus',
    //       var: 'ElementPlus',
    //       path: 'https://unpkg.com/element-plus@2.3.5',
    //       css: 'https://unpkg.com/element-plus/dist/index.css'
    //     },
    //     {
    //       name: 'vue-demi',
    //       var: 'VueDemi',
    //       path: 'https://unpkg.com/vue-demi@0.13.7'
    //     },
    //     {
    //       name: '@wangeditor/editor',
    //       var: 'wangEditor',
    //       path: 'https://unpkg.com/@wangeditor/editor@latest/dist/index.js',
    //       css: 'https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css'
    //     }
    //   ]
    // }),
    vueSetupExtend(),
    // ...
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  build: {
    rollupOptions: {
      // 👇 告诉打包工具 "vue-demi" 也是外部依赖项 👇
      external: ['vue', 'element-plus', 'vue-demi', '@wangeditor/editor'],
      plugins: [
        externalGlobals({
          vue: 'Vue',
          'element-plus': 'ElementPlus',
          // 👇 配置 vue-demi 全局变量 👇
          'vue-demi': 'VueDemi',
          '@wangeditor/editor': 'wangEditor'
        })
      ]
    }
  }
})
